package net.sky.oa.dao.system.impl;

import java.util.ArrayList;
import java.util.List;

import net.sky.oa.dao.SkyBaseDaoImpl;
import net.sky.oa.dao.system.MyGroupEmplDao;
import net.sky.oa.model.MyGroupEmpl;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.htsoft.core.web.paging.PagingBean;

@SuppressWarnings("unchecked")
@Repository
public class MyGroupEmplDaoImpl extends SkyBaseDaoImpl<MyGroupEmpl> implements
	MyGroupEmplDao {
	
	public MyGroupEmplDaoImpl() {
		super.setPersistType(MyGroupEmpl.class);
	}

	public List<MyGroupEmpl> findAllGroupEmpl() {
		String hql = "from MyGroupEmpl s";
		return findByHql(hql);
	}
	
	public List  findMyEmpl(String groupId) {
		String hql = "select distinct sm.id from MyGroupEmpl s,SysEmployee sm where s.user.id=sm.id and  s.group.id="+ groupId;
	    return findByHql(hql);
	}
	
	public List<MyGroupEmpl>  findMyEmpl(Long userId,String groupId,String name, String enname, String sex, PagingBean pb) {
		StringBuffer hql = new StringBuffer("from MyGroupEmpl s where s.group.empl.id="+userId+"and s.user.status = 1");
		List list = new ArrayList();
		if (StringUtils.isNotEmpty(groupId)) {
			hql.append(" and s.group.id = ?");
			list.add(Long.valueOf(groupId));
		}
		if (StringUtils.isNotEmpty(name)) {
			hql.append(" and s.user.name like ?");
			list.add("%" + name + "%");
		}
		if (StringUtils.isNotEmpty(enname)) {
			hql.append(" and s.user.enname like ?");
			list.add("%" + enname + "%");
		}
		if (StringUtils.isNotEmpty(sex)) {
			hql.append(" and s.user.sex = ?");
			list.add(sex);
		}
		return findByHql(hql.toString(), list.toArray(),pb);
	}
	
	public List  findMyEmpl(Long userId,String name, String sex, String groupId, String school, String dept, String post, PagingBean pb) {
		StringBuffer hql = new StringBuffer("select distinct sm from MyGroupEmpl s,SysEmployee sm where s.user.id=sm.id and  s.group.empl.id="+userId+"and s.user.status = 1");
		    List list = new ArrayList();
		    if (StringUtils.isNotEmpty(groupId)) {
		    	hql.append(" and s.group.id = ?");
		    	list.add(Long.valueOf(groupId));
		    }
		    if (StringUtils.isNotEmpty(name)) {
		      hql.append(" and s.user.name like ?");
		      list.add("%" + name + "%");
		    }
		    if (StringUtils.isNotEmpty(sex)) {
				hql.append(" and s.user.sex = ?");
				list.add(sex);
			}
		    if (StringUtils.isNotEmpty(school)) {
		      hql.append(" and s.user.sysschool.name like ?");
		      list.add("%" + school + "%");
		      
		    }
		    if (StringUtils.isNotEmpty(dept)) {
		    	hql.append(" and s.user.dept.name like ?");
		    	list.add("%" + dept + "%");
		    }
		    if (StringUtils.isNotEmpty(post)) {
		    	hql.append(" and s.user.post.name like ?");
		    	list.add("%" + post + "%");
		    }
		    return findByHql(hql.toString(), list.toArray(),pb);
	  }
	
}